
#define F_CPU 1000000UL
#include <util/delay.h>   //  library for delay purpose
#include <avr/interrupt.h> // library for interrupt purpose
#include <avr/io.h>

int volatile TMP=0;  //volatile variable for unstable counter

void dlyf();  // delay function prototype


int main(void)
{
	int cnt=0; // counter variable and set to zero
	DDRD = 0b1011; // DDRD(0,1,3) as output
	DDRB = 0xFF; // make DDRB as output
	sei(); //  interrupts
	EICRA =(1<<ISC01); // set interrupt on ISC01
	EIMSK =(1<<INT0); // enable INT0


	while(1)
	{
		dlyf(); //delay
		if(PORTD&(1<<PD0)) // loop if port 0 is set
		{
			dlyf(); // start delay
			PORTB = ++cnt; // counter will course toggle on PD0's falling edge
			if(cnt>=255) // if counter reach 255 reset counter
			cnt=0; // reset counter
			if((cnt%20)==0) //PD1 led turn on for every 1pth falling purlse reached
			PORTD ^=1<<PD1;	 // turn on PD1 LED
		}
		
	}

	
}
void dlyf() // delay function
{
	_delay_ms(250.0); // delay time is 250ms per cycle
	PORTD ^= 1<<PD0; // toggle PD 0 after delay
}

ISR (INT0_vect) // interrupt function
{
	TMP++;  // increase global counter
	if(TMP ==2){ //counter=2 turn on PD3 then reset counter
		PORTD = (1<<PD3);
		TMP=0;
	}
}

